#include<iostream>
#include<cstring>
using namespace std;
const int N = 310;
int dist[N];
int w[N][N];
bool st[N];
int n;
int prim()
{
    memset(dist, 0x3f, sizeof dist);
    dist[0] = 0;
    int res = 0;
    for (int i = 0; i < n + 1; i++)
    {
        int t = -1;
        for (int j = 0; j <= n; j++)
        {
            if (!st[j] && (t == -1 || dist[j] < dist[t])) t = j;
        }
        st[t] = true;
        res += dist[t];
        for (int j = 0; j <= n; j++) dist[j] = min(dist[j], w[t][j]);
    }
    return res;
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        int c;
        cin >> c;
        w[i][0] = w[0][i] = c;
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            int c;
            cin >> c;
            w[i][j] = w[j][i] = c;
        }
    }
    int res = prim();
    cout << res << endl;
    return 0;
}